﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Write.API.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.API.Application.CommandHnadler
{
    /// <summary>
    /// 商品删除查询命令处理
    /// </summary>
    public class GoodsDeleteCommandHandler : IRequestHandler<GoodsDeleteCommand,ApiResult<object>>
    {
        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<GoodsInfo> goodsReposistory;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;


        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodsReposistory"></param>
        /// <param name="mapper"></param>
        public GoodsDeleteCommandHandler(IBaseRepository<GoodsInfo> goodsReposistory,IMapper mapper)
        {
            this.goodsReposistory = goodsReposistory;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(GoodsDeleteCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnums.删除成功;
            result.Msg = ApiEnums.删除成功.ToString();
            var use = goodsReposistory.GetAll().FirstOrDefault(x => x.GoodsId == request.GoodsId);
            use.IsDelete = true;
            var i = goodsReposistory.Update(use);
            if (i > 0)
            {
                result.Code = ApiEnums.删除成功;
                result.Msg = ApiEnums.删除成功.ToString();
                result.Data = i;
            }
            else
            {
                result.Code = ApiEnums.删除失败;
                result.Msg = ApiEnums.删除失败.ToString();
                result.Data = i;
            }
            return Task.FromResult(result);
        }
    }
}
